This page last changed on Jul 10, 2004 by unkyaku.

下列校验器包含在缺省的validators.xml中:
名称使用JavaScript描述
#required 字段必须有值(非空)
#requiredstringx字段值必须非空且长度 > 0
#intx字段值必须是整数并在一定范围内
#date 字段值必须是日期(日期格式依赖于区域设置)并在一定范围内
#expression 给定一个OGNL表达使并使用值栈进行运算, 返回值必须是true. 可用于跨字段校验. 错误将作为活动错误
#fieldexpression 给定一个OGNL表达使并使用值栈进行运算, 返回值必须是true. 类似于expression但错误将作为字段错误
#emailx字段值必须是合法的电子邮件地址
#urlx字段值必须是合法的URL
visitor 允许将校验转移给活动的属性并使用它自己的校验文件
conversion 把ActionContext中的类型转换错误作为活动的字段错误. 与WebWorkConversionErrorInterceptor的功能相同

注意: 如果你使用自己的validators.xml文件, 上述名字可以修改.

required

SimpleAction-validation.xml:
<validators>
    <field name="bar">
        <field-validator type="required">
            <message>You must enter a value for bar.</message>
        </field-validator>
    </field>
</validators>

#top

requiredstring

LoginAction-validation.xml:

<validators>
    <field name="userName">
        <field-validator type="requiredstring">
            <message>You must enter an username.</message>
        </field-validator>
    </field>
</validators>

The error is shown if request parameter userName is missing or an empty string

#top

int

<validators>
    <field name="foo">
        <field-validator type="int">
            <param name="min">0</param>
            <param name="max">100</param>
            <message key="foo.range">Could not find foo.range!</message>
        </field-validator>
    </field>
</validators>

#top

date

<validators>
    <field name="startDate">
        <field-validator type="date">
            <param name="min">12/22/2002</param>
            <param name="max">12/25/2002</param>
            <message>The date must be between 12-22-2002 and 12-25-2002.</message>
        </field-validator>
    </field>
</validators>

#top

expression

<validators>
    <validator type="expression">
        <param name="expression">foo > bar</param>
        <message>Foo must be greater than Bar. Foo = ${foo}, Bar = ${bar}.</message>
    </validator>
</validators>

校验器不仅检查一个字段. 如果表达式中包含xml字符, 可以使用CDATA.

#top

fieldexpression

<validators>
     <field name="productCode">
        <field-validator type="fieldexpression">
            <param name="expression">name.length() == 5</param>
            <message>Product code must be 5 characters, it is currently '${productCode}'</message>
        </field-validator>
    </field>
</validators>

#top

email

<validators>
    <field name="email">
        <field-validator type="email">
            <message>You must enter a valid email address.</message>
        </field-validator>
    </field>
</validators>

address必须符合格式xxx@yyy.com|net|gov|org|edu|info|mil|biz|tv|…

#top

url

<validators>
     <field name="companyUrl">
        <field-validator type="url">
            <message>You must enter a valid URL.</message>            
        </field-validator>
    </field>
</validators>

#top

Document generated by Confluence on Dec 14, 2004 16:36